package cn.com.gridinfo.sooc.core.login.activity.forgetpwd;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.jeremy.arad.utils.AnimUtil;
import com.jeremy.arad.utils.Log;
import com.jeremy.arad.utils.StringUtils;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.SoocApplication;
import cn.com.gridinfo.sooc.base.BaseSoocActivity;
import cn.com.gridinfo.sooc.core.login.common.CodeConst;
import cn.com.gridinfo.sooc.core.login.dao.ForgetDao;
import cn.com.gridinfo.sooc.utils.MessageUtils;

/**
 * 显示用户基本信息，选择找回方式
 * By 刘晓晨
 */
public class BackPwdModeActivity extends BaseSoocActivity implements View.OnClickListener {

    @InjectView(R.id.phoneNumber)
    TextView phoneNumber; //用户手机号码
    @InjectView(R.id.emailAddress)
    TextView emailAddress; //用户邮箱地址

    @InjectView(R.id.pnumberFind)
    RelativeLayout pnumberFind;//手机号码找回
    @InjectView(R.id.emailFind)
    RelativeLayout emailFind; // 邮箱找回
    @InjectView(R.id.pwdPrtFind)
    RelativeLayout pwdPrtFind; //密保找回
    @InjectView(R.id.backToPhoneNumber)
    TextView backToPhoneNumber;//通过手机号找回
    @InjectView(R.id.backToEmail)
    TextView backToEmail;//通过邮箱找回
    @InjectView(R.id.backToMiBao)
    TextView backToMiBao;//通过密保找回

    private String userAccount;//用户账号
    private String xtdz;//节点域名
    private ForgetDao dao;
    private String pNumber = "";//定义全局变量 手机号码
    private String email;//定义全局变量，邮箱地址
    String newPnumber;//获得用户电话
    String newEmail;//获得用户邮箱
    String uid;//获得用户Id

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.back_pwd_mode_activity);
        SoocApplication.getContext().addActivity(this);
        ButterKnife.inject(this);
        pnumberFind.setOnClickListener(this);
        emailFind.setOnClickListener(this);
        pwdPrtFind.setOnClickListener(this);
        dao = new ForgetDao(this);
        userAccount = getIntent().getStringExtra("account");//拿到上个页面传来的用户账号
        xtdz = getIntent().getStringExtra("xtdz");//拿到上个页面传来的节点域名
        Log.d("账号：" + userAccount);
        Log.d("节点域名：" + xtdz);
        dao.setXtdz(xtdz);
        //请求接口，判断是否设置了密保问题

        initView(userAccount);
    }

    //设置标题
    @Override
    public String setupToolBarTitle() {
        return "找回方式";
    }

    @Override
    public boolean setupToolBarLeftButton(ImageView leftButton) {
        return true;
    }

    //ToolBar的back键
    @OnClick(R.id.toolbar_leftbtn)
    void onToolbarLeftClick() {
        finish();
    }

    //请求接口
    private void initView(String userAccount) {
        dao.getForgetUserInfo(userAccount, userAccount + "0S3FV7PH8MLJT6IOAG2ECWQNXUBR1YZ459KD"
        );
    }

    //请求成功返回数据
    @Override
    public void onRequestSuccess(int requestCode) {
        super.onRequestSuccess(requestCode);
        switch (requestCode) {
            case CodeConst.FORGETPWD_INFO://显示用户基本信息
                pNumber = dao.forgetUserInfo.getCellphone();//拿到手机号
                email = dao.forgetUserInfo.getEmail();//拿到邮箱
                uid = dao.forgetUserInfo.getUid();//拿到uid
                Log.d("手机号" + pNumber);
                Log.d("邮箱" + email);
                Log.d("uid" + uid);
                dao.mibaoQuestion(uid, uid + "0S3FV7PH8MLJT6IOAG2ECWQNXUBR1YZ459KD");
                show();
                break;
            case CodeConst.MIBAOWENTI://检测是否设置了密保问题
                if (dao.status.equals("0")) {//设置了密保问题，正常显示，可点击

                } else {
                    //未设置，置灰，不能点击
                    backToMiBao.setTextColor(Color.LTGRAY);
                    pwdPrtFind.setClickable(false);
                }
                break;
            case CodeConst.NEWPASSWORD:
                MessageUtils.showRightImageShortToast(this, dao.getStatusCode());
                break;
            case CodeConst.NEWPASSWORDBYEMAIL://邮箱找回
                new MaterialDialog.Builder(this)//弹框提示
                        .backgroundColor(Color.WHITE)
                        .content("邮件已发送，请登录邮箱进行验证！")
                        .contentColorRes(R.color.color_666)
                        .positiveText(R.string.agree).positiveColorRes(R.color.colorAccent)
                        .onPositive(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                            }
                        })
//                        .negativeText()
//                        .negativeColor(Color.LTGRAY)
                        .show();
                break;
        }
    }

    @Override
    public void onRequestFaild(int requestCode, String errorNo, String errorMessage) {
        super.onRequestFaild(requestCode, errorNo, errorMessage);
        MessageUtils.showImageShortToast(this, dao.getStatusCode());
    }

    //显示基本信息
    private void show() {
        if (!StringUtils.isNull(pNumber) && pNumber.length() == 11) {
            String beforePhone = pNumber.substring(0, 3);//拿到电话号码前3位
            String afterPhone = pNumber.substring(7, 11);//拿到电话号码的后4位
            newPnumber = beforePhone + "****" + afterPhone;
        }
        if (!StringUtils.isNull(email) && email.length() > 2 && email.contains("@")) {
            String beforeEmail = email.substring(0, 2);//拿到邮箱地址的前两位
            String emailTyp = email.substring(email.indexOf("@"));//拿到邮箱类型
            newEmail = beforeEmail + "****" + emailTyp;
        }

        if (pNumber != null) {
            //不为空 显示
            phoneNumber.setText(newPnumber);
        } else {
            phoneNumber.setText("无");
            //置灰不能显示
            backToPhoneNumber.setTextColor(Color.LTGRAY);
            backToPhoneNumber.setClickable(false);
        }
        if (email != null) {
            //不为空 显示
            emailAddress.setText(newEmail);
        } else {
            emailAddress.setText("无");
            //置灰不能点击
            backToEmail.setTextColor(Color.LTGRAY);
            backToEmail.setClickable(false);
        }


    }

    //  监听点击事件
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.pnumberFind://点击 手机号找回
                Intent i = new Intent(this, BackPwdTelActivity.class);
                i.putExtra("phoneNumber", newPnumber);//传递手机号到下一个Activity（带****）
                i.putExtra("realNumber", pNumber);//传递手机号到下一个Activity（不带****）
                i.putExtra("way", "111");//标志进入下一个界面的方式
                i.putExtra("xtdz", xtdz);//将节点域名传递
                i.putExtra("uid", uid);
                startActivity(i);
                AnimUtil.intentSlidIn(this);
                break;
            case R.id.pwdPrtFind://点击 密保找回
                Intent n = new Intent(this, BackPwdMiBaoActivity.class);
                n.putExtra("way", "111");//标志进入下一个界面的方式 111：找回密码进入
                n.putExtra("xtdz", xtdz);//节点域名传出
                n.putExtra("uid", uid);//将uid传出
                startActivity(n);
                AnimUtil.intentSlidIn(this);
                break;
            case R.id.emailFind:// 点击 邮箱找回
                dao.forgetPwdByEmail(uid, "email", uid + "email" + "0S3FV7PH8MLJT6IOAG2ECWQNXUBR1YZ459KD");
                break;

        }
    }
}
